package com.ysd.lis.service.inv.impl;

import cn.hutool.extra.pinyin.PinyinUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.inv.InvInstrManage;
import com.ysd.lis.entity.inv.InvStoreManage;
import com.ysd.lis.entity.inv.InvUserManage;
import com.ysd.lis.mapper.inv.InvInstrManageMapper;
import com.ysd.lis.mapper.inv.InvStoreManageMapper;
import com.ysd.lis.mapper.inv.InvUserManageMapper;
import com.ysd.lis.request.CommonPageDto;
import com.ysd.lis.service.inv.InvStoreManageService;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 仓库管理 服务实现类
 * </p>
 *
 * @author WH
 * @since 2024-09-09
 */
@Service
public class InvStoreManageServiceImpl extends ServiceImpl<InvStoreManageMapper, InvStoreManage> implements InvStoreManageService {

    @Autowired
    private InvStoreManageMapper invStoreManageMapper;
    @Autowired
    private InvUserManageMapper invUserManageMapper;
    @Autowired
    private InvInstrManageMapper invInstrManageMapper;

    @Override
    public Result findStoreManageList(CommonPageDto param) {
        LambdaQueryWrapper<InvStoreManage> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(InvStoreManage::getDelFlag, 0);

        if (StringUtils.isNotBlank(param.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(InvStoreManage::getStoreName, param.getSeachValue()).or().like(InvStoreManage::getStoreCode, param.getSeachValue()));
        }
        lambdaQueryWrapper.orderByAsc(InvStoreManage::getSeq);
        List<InvStoreManage> invStoreManages = invStoreManageMapper.selectList(lambdaQueryWrapper);
        return Result.succ(1, "查询成功", invStoreManages);
    }

    @Override
    public Result findStoreManageTree() {
        LambdaQueryWrapper<InvStoreManage> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(InvStoreManage::getDelFlag, 0);
        List<InvStoreManage> invStoreManageList = invStoreManageMapper.selectList(lambdaQueryWrapper);

        Map<String, Object> resultMap = new HashMap<>();

        //只获取机构树
        List<Map> invStoreTree = new ArrayList<>();
        for(InvStoreManage o : invStoreManageList){
            if(ToolsUtils.isEmpty(o.getParentId())){
                Map map = new HashMap();
                map.put("id", o.getId());
                map.put("storeCode", o.getStoreCode());
                map.put("storeName", o.getStoreName());
                map.put("parentId", o.getParentId());
                map.put("level", o.getLevel());
                invStoreTree.add(map);
            }
        }
        for (Map m : invStoreTree) {
            List<Map> child = getChild(m, invStoreManageList);
            m.put("children", child);
        }
        resultMap.put("invStoreTree", invStoreTree);
        return Result.succ(1, "查询成功", resultMap);
    }

    private List<Map> getChild(Map m, List<InvStoreManage> invStoreManageList) {
        List<Map> result = new ArrayList();

        for (InvStoreManage to : invStoreManageList) {
            if (ToolsUtils.isNotEmpty(to.getParentId()) && to.getParentId().equals(m.get("id"))) {
                Map map = new HashMap();
                map.put("id", to.getId());
                map.put("storeCode", to.getStoreCode());
                map.put("storeName", to.getStoreName());
                map.put("parentId", to.getParentId());
                map.put("level", to.getLevel());
                map.put("parentName", m.get("storeName"));
                result.add(map);
            }
        }

        for (Map ma : result) {
            ma.put("children", getChild(ma, invStoreManageList));
        }
        if (result.size() == 0) {
            return null;
        }
        return result;
    }

    @Override
    public Result getJoinOrMoveStore(CommonPageDto params) {
        LambdaQueryWrapper<InvStoreManage> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(InvStoreManage::getDelFlag, 0);
        lambdaQueryWrapper.and(wrapper ->
                wrapper.eq(InvStoreManage::getIsJoinStore, 1)
                        .or().eq(InvStoreManage::getIsMoveStore, 1));
        List<InvStoreManage> invStoreManages = invStoreManageMapper.selectList(lambdaQueryWrapper);
        List<InvStoreManage> isJoinList = invStoreManages.stream()
                .filter(invStoreManage -> invStoreManage.getIsJoinStore().equals(new BigDecimal(1)))
                .collect(Collectors.toList());
        List<InvStoreManage> isMoveList = invStoreManages.stream()
                .filter(invStoreManage -> invStoreManage.getIsMoveStore().equals(new BigDecimal(1)))
                .collect(Collectors.toList());
        Map<String, List<InvStoreManage>> map=new HashMap<>();
        map.put("isJoinList", isJoinList);
        map.put("isMoveList", isMoveList);
        return Result.succ(1, "查询成功", map);
    }

    @Override
    public Result saveOrUpdateStoreManage(InvStoreManage invStoreManage) {

        String pinyinName = PinyinUtil.getPinyin(invStoreManage.getStoreName(), "");
        invStoreManage.setStorePyName(pinyinName);

        boolean b = this.saveOrUpdate(invStoreManage);
        if (b) {
            return Result.succ(1, "成功", invStoreManage);
        } else {
            return Result.fail(400, "失败", invStoreManage);
        }
    }

    @Override
    public Result deleteStoreManage(InvStoreManage invStoreManage) {
        invUserManageMapper.delete(new LambdaQueryWrapper<>(InvUserManage.class).eq(InvUserManage::getStoreId, invStoreManage.getId()));
        invInstrManageMapper.delete(new LambdaQueryWrapper<>(InvInstrManage.class).eq(InvInstrManage::getStoreId, invStoreManage.getId()));
        int deleteById = invStoreManageMapper.deleteById(invStoreManage.getId());
        if (deleteById > 0) {
            return Result.succ(1, "删除成功", "");
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }


}
